# ---------------阻塞IO循环收发消息----------
from socket import *

clent_sk=socket(AF_INET,SOCK_STREAM)

conn=clent_sk.connect(("192.168.0.103",8888))
try:
    clent_sk.send("hello server".encode("utf-8"))

    data = clent_sk.recv(1024)
    print("服务端发来的消息是",data.decode("utf-8"))
    clent_sk.close()
except Exception as e:
    print(e)


# -------------非阻塞IO----------------------
import time,socket
sk=socket.socket()
while True:
    sk.connect(("192.168.0.103",8080))
    print("hello")
    sk.sendall(bytes("hello","utf-8"))
    time.sleep(2)
    break

# ---------------select IO 多路复用----------------------
import socket,time,select
sk=socket.socket()
# sk.connect(("192.168.0.103",8080))
sk.bind(('localhost',5000))
print("*************")

# ---------------select IO 多路复用----------------------
<<<<<<< HEAD
# import socket
# sk = socket.socket()
# sk.connect(("192.168.0.103",8080))
# sk.connect(('172.16.8.55',8090))
# while 1:
#     inp = input(">>>>>>>")
#     sk.send(inp.encode("utf8"))
#     sk.send("你好 ！！server")
#     data=sk.recv(1024)
#     print(data.decode("utf8"))
# print("*************")
# ---------------------------------------------------
import socket
import sys,os
class conntClient(object):
    def __init__(self):
        self.args=sys.argv
        if len(self.args):
            self.port=(self.args[1],int(self.args[2]))
        else:
            self.port("localhost",1234)
        self.creat_socket()
        self.command_fanout()
    def creat_socket(self):
        try:
            self.sk = socket.socket()
            self.sk.connect(self.port)
            print("connent sucess")
        except Exception as e:
            print("error ",e)

    def command_fanout(self):
        while True:
            cmd =input(">>>>>").split()
            if cmd =="exit()":
                break
            cmd,file = cmd.split()
            if hasattr(self,cmd):
                func = getattr(self,cmd)
                func(cmd,file)  #put ("put","a.avi")
            else:
                print("调用失败")

    def put(self,cmd,file):
        if os.path.isfile(file):
            fileName = os.path.basename(file)
            fileSize = os.path.getsize(file)
            fileinfo = "%s|%s|%s" % (cmd,fileName,fileSize)
            self.sk.send(bytes(fileinfo,encoding="utf-8"))
            recvStatus = self.sk.recv(1024) # 接受ok
            print("recvStatus",recvStatus)
            hasSend = 0
            if str(recvStatus,encoding="utf-8") == "ok":
                with open(file,'rb')as f:
                    while fileSize > hasSend:
                        contant = f.readline(1024)
                        recv_size = len(contant)
                        self.sk.send(contant)
                        hasSend += recv_size
                        s = str(int(hasSend/fileSize*100))+"%"
                        print("文件正在上传" + fileName+"已上传"+s)
                    print("文件上传完毕" % fileName)
            else:
                print("文件不存在")

    def get(self,cmd,file):
        if not os.path.isfile(file):
            fileName = os.path.basename(file)   # 发送的文件名字
            fileSize = os.path.getsize(file)
            fileinfo = "%s|%s|%s" % (cmd, fileName, 0)

            self.sk.send(bytes(fileinfo, encoding="utf-8"))

            recv_data = self.sk.recv(1024)  # 接受 server 端发送的 filesize 文件的大小

            print("recv_data", recv_data)
            hasrecv = 0
            filename,filesize = str(recv_data, encoding="utf-8").split("|")
            self.recv.dict = {self.sk :{"filename": filename, "filesize": int(filesize)}}  # 接受的文件信息
            if str(self.recv.dict[self.sk][filename],encoding="utf-8") == fileName: # 比较文件 名字相同 则开始下载
                with open(file, 'rb')as f:
                    while fileSize > hasrecv:
                        recv_data = self.sk.recv(1024)
                        contant = f.write(recv_data)
                        recv_size = len(contant)
                        self.sk.send(contant)
                        hasrecv += recv_size
                        s = str(int(hasrecv / fileSize * 100)) + "%"
                        print("文件正在下载" + fileName + "下载完成" + s)
                    print("文件下载完毕" % fileName)
        else:
            print("文件已存在、无需下载")

if __name__ == '__main__':
    conntClient()
=======
import socket
sk = socket.socket()
sk.connect(("192.168.0.103",8080))
sk.connect(('172.16.8.55',8090))
while 1:
    inp = input(">>>>>>>")
    sk.send(inp.encode("utf8"))
    sk.send("你好 ！！server")
    data=sk.recv(1024)
    print(data.decode("utf8"))
print("*************")

>>>>>>> aee635d942402186f1fbea0dfa29a38c3ebc31ac
